/**
 */
package it.rainbowbreeze.polygen.util;

import it.rainbowbreeze.polygen.common.AppEnv;
import it.rainbowbreeze.polygen.common.AppEnv.ObjectsFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.test.RenamingDelegatingContext;

/**
 * @author Alfredo "Rainbowbreeze" Morresi
 *
 */
public class TestHelper {
    // ------------------------------------------ Private Fields
    private static Class<? extends ObjectsFactory> mLastObjectFactoryClass = null;
    private static ObjectsFactory defaultObjectsFactory = new ObjectsFactory();  

    // -------------------------------------------- Constructors

    
    // --------------------------------------- Public Properties

    
    // ------------------------------------------ Public Methods
    public static boolean init(Context context) {
        return init(context, defaultObjectsFactory);
    }
    
    public static boolean init(
            Context context,
            AppEnv.ObjectsFactory overriddingObjectsFactory) {

        //checks for previous {@link AppEnv.ObjectsFactory} used
        if (null != mLastObjectFactoryClass) {
            if (overriddingObjectsFactory.getClass().equals(mLastObjectFactoryClass))
                return false;
        }

        //initializes the environment
        AppEnv.i(context, overriddingObjectsFactory);
        mLastObjectFactoryClass = overriddingObjectsFactory.getClass();
        
        //custom object initialization
        return true;
    }
    
    /**
     * Creates a mock context where all files have a different name suffix,
     * in order to avoid conflicts with true application files.
     * 
     * @param context
     * @return
     */
    public static Context getMockContext(Context context) {
       return new RenamingDelegatingContext(context, "test.");
    }
    
    /**
     * Read picture from a resource and put it in a byte array
     * 
     * @param context
     * @param resourceId id of a resource under the raw resource directory
     * @return
     */
    public static byte[] getContentFromRawResource(Context context, int resourceId) {
        InputStream rawInputStream = context.getResources().openRawResource(resourceId);
        
        BufferedInputStream inputStr = null;
        BufferedOutputStream output = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // Open streams.
            inputStr = new BufferedInputStream(rawInputStream);
            output = new BufferedOutputStream(baos);
            // Write image contents to response.
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = inputStr.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStr != null) {
                try {
                    inputStr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return baos.toByteArray();
    }

    
    // ----------------------------------------- Private Methods

}
